package com.diorsunion.hedge.web.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * Created by harley-dog on 2016/5/7.
 */
public class CharacterEncodingFilter implements Filter {
    private String encoding = "UTF-8";

    private boolean forceEncoding = false;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        if (this.encoding != null && (this.forceEncoding || servletRequest.getCharacterEncoding() == null)) {
            servletRequest.setCharacterEncoding(this.encoding);
            if (this.forceEncoding) {
                servletRequest.setCharacterEncoding(this.encoding);
            }
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
